package com.zabra.zl.ui.profit;


import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zabra.zl.Config;
import com.zabra.zl.R;
import com.zabra.zl.base.BaseMvpActivity;
import com.zabra.zl.data.ProfitBean;
import com.zabra.zl.data.SimpleBean;
import com.zabra.zl.ui.profit.contract.ProfitContract;
import com.zabra.zl.ui.profit.presenter.ProfitPresenter;
import com.zabra.zl.utils.DialogUtils;
import com.zabra.zl.utils.ShowDialogUtils;
import com.zabra.zl.utils.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 提盈
 */
public class ProfitActivity extends BaseMvpActivity<ProfitPresenter> implements ProfitContract.View {


    @BindView(R.id.back)
    LinearLayout back;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.tv_sure)
    TextView tvSure;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_chicang)
    TextView tvChicang;
    @BindView(R.id.tv_canuser)
    TextView tvCanuser;
    @BindView(R.id.tv_fudong)
    TextView tvFudong;
    @BindView(R.id.et_input)
    EditText etInput;
    private int orderId;
    private Dialog dialog;
    private String strMoney;

    @Override
    public int getLayoutId() {
        return R.layout.activity_profit;
    }

    @Override
    public void initView() {
        orderId = getIntent().getExtras().getInt("orderid");
        mPresenter = new ProfitPresenter();
        mPresenter.attachView(this);
        mPresenter.getData(orderId);
    }

    @Override
    public void showLoading() {
        dialog = DialogUtils.showDialog_progressbar(this);
    }

    @Override
    public void hideLoading() {
        DialogUtils.closeDialog(dialog);
    }

    @Override
    public void onError(Throwable throwable) {
        DialogUtils.closeDialog(dialog);
    }

    @Override
    public void onSuccess(ProfitBean bean) {
        if (bean.getRespCode().equals(Config.SUCCESS)) {
            tvMoney.setText(bean.getRespData().getProfitAmt() + "");
            tvFudong.setText(bean.getRespData().getFloatScale() + "");
            tvChicang.setText(bean.getRespData().getAllowAmt() + "");
            tvCanuser.setText(bean.getRespData().getNowAmt() + "");
        } else if (bean.getRespCode().equals(Config.TOKENOUT)) {
            ShowDialogUtils.restLoginDialog(this);
        } else {
            ToastUtil.showToast(this, bean.getRespDesc());
        }
    }

    @Override
    public void onSuccess(SimpleBean bean) {
        if (bean.getRespCode().equals(Config.SUCCESS)) {
            ToastUtil.showToast(this, bean.getRespDesc());
            finish();
        } else if (bean.getRespCode().equals(Config.TOKENOUT)) {
            ShowDialogUtils.restLoginDialog(this);
        } else if (bean.getRespCode().equals(Config.WAITING)) {
            ToastUtil.showToast(this, bean.getRespDesc());
            finish();
        } else {
            ToastUtil.showToast(this, bean.getRespDesc());
        }
    }

    private void showDialog() {
        final AlertDialog dialog = new AlertDialog.Builder(this).create();
        LayoutInflater layoutInflater = this.getLayoutInflater();
        View view = layoutInflater.inflate(R.layout.dialog_profit, null);
        dialog.setView(view);
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
        Window window = dialog.getWindow();
        window.setBackgroundDrawableResource(android.R.color.transparent);
        TextView tvContent = view.findViewById(R.id.dialog_content);
        tvContent.setText("您即将提盈" + strMoney + "元到现金账户，继续操作？");
        view.findViewById(R.id.iv_cancle).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        view.findViewById(R.id.tv_cancle).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        view.findViewById(R.id.tv_sure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mPresenter.getSubmitData(orderId, strMoney);
                dialog.dismiss();
            }
        });
    }


    @OnClick({R.id.back, R.id.tv_sure})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.tv_sure:
                strMoney = etInput.getText().toString().trim();
                if (TextUtils.isEmpty(strMoney)) {
                    ToastUtil.showToast(this, "请输入提盈金额");
                } else {
                    showDialog();
                }
                break;
        }
    }

}
